package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2022/8/2.
 * 安全计划管理
 */
@Component
public class SafetyplanHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;

    //统计作业计划里边，人数选择，人数之和
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("gq_splexiconlink".equals(objName)) {
            //危险编号
            String hazardnum = MapUtils.getString(data, "hazardnum");
            //安全计划编号
            String safetyplannum = MapUtils.getString(data, "safetyplannum");
            //危险唯一标识
            String hazardid = MapUtils.getString(data, "hazardid");
            String safetyplanid = null;
            //安全计划唯一标识 得查一下了
            String sql2 = "select safetyplanid from gq_safetyplan where safetyplannum = '" + safetyplannum+"'";
            List<Map<String, Object>> safetyplanList = handlerService.findBySql(sql2);
            if (safetyplanList.size() > 0) {
                if( safetyplanList.get(0)!=null&&safetyplanList.get(0).get("safetyplanid")!=null){
                    safetyplanid = safetyplanList.get(0).get("safetyplanid").toString();
                }


            }
            /*
            * 把危险子表预防措施数据查出来插入到 安全计划子表预防措施中
            * */
            //1 通过危险编号，查询预防措施和隔离集合
            String sql = "select hazardprecnum,abstract,hazardprecid from gq_hazardprec where hazardnum= '"+hazardnum+"'";
            List<Map<String, Object>> mapList = handlerService.findBySql(sql);
            if (mapList.size() > 0) {
                //删除危险子表--预防措施信息
                String sql5 = "delete from gq_safetymeasure where hazardnum='"+hazardnum+"'";
                handlerService.updateBySql(sql5);

                for (Map<String, Object> objectMap : mapList) {
                    String hazardprecnum = objectMap.get("hazardprecnum").toString();
                    String abstractdesc = objectMap.get("abstract").toString();
                    //危险子表中，预防措施唯一标识
                    String hazardprecid = null;
                    if (objectMap.get("hazardprecid") != null) {
                        hazardprecid = objectMap.get("hazardprecid").toString();
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("hazardprecnum", hazardprecnum);
                    map.put("abstract", abstractdesc);
                    map.put("safetyplannum", safetyplannum);
                    map.put("hazardprecid", hazardprecid);
                    map.put("hazardnum", hazardnum);
                    //危险唯一标识
                    map.put("hazardid", hazardid);
                    map.put("safetyplanid", safetyplanid);
                    //新增数据时，只需要把做需要的字段新增就可以，其他像部门，时间，创建人等信息会自动添加
                    handlerService.insert("gq_safetymeasure", map);

                }
            }

            /*
            * 把危险中子表 隔离信息查出来，插入到安全计划 子表隔离表中
            *
            * */
            String sql3 = "select tagoutnum,abstract,assetnum,location,requiredstate from gq_safetylexicon where hazardnum = '"+hazardnum+"'";
            List<Map<String, Object>> tagoutlineList = handlerService.findBySql(sql3);
            if (tagoutlineList.size() > 0) {
                //删除危险子表--隔离信息
                String sql6 = "delete from gq_tagoutline where hazardnum='"+hazardnum+"'";
                handlerService.updateBySql(sql6);
                for (Map<String, Object> objectMap : tagoutlineList) {
                    String tagoutnum = objectMap.get("tagoutnum").toString();
                    String abstractdesc = objectMap.get("abstract").toString();
                    String assetnum = objectMap.get("assetnum").toString();
                    String location = objectMap.get("location").toString();
                    String requiredstate = objectMap.get("requiredstate").toString();
                    Map<String, Object> map = new HashMap<>();
                    map.put("tagoutlinenum", tagoutnum);
                    map.put("abstract", abstractdesc);
                    map.put("assetnum", assetnum);
                    map.put("location", location);
                    map.put("safetyplannum", safetyplannum);
                    map.put("safetyplanid", safetyplanid);
                    map.put("requiredstate", requiredstate);
                    //危险编号
                    map.put("hazardnum", hazardnum);
                    handlerService.insert("gq_tagoutline", map);

                }
            }


            // List<Map<String, Object>> mapList = handlerService.findBySql(" select sum(quantity) as quantitys from gq_joblabor where jpnum =" + jpnum);


        }
    }


    //保存前，判断是否修改了危险编号  目前没办法修改危险编号的时候，删除数据，因为多条危险子表数据，无法判断修改的那一条。所以不允许修改，只能新增
    //或者删除
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
//        if ("gq_splexiconlink".equals(objName)&&isUpdate) {
//            //根据安全计划编号，查询原数据有没有危险编号，如果有，根据安全计划编号和旧危险编号删除子表中预防措施和隔离
//            String safetyplannum = MapUtils.getString(data, "safetyplannum");
//            //安全计划唯一标识 得查一下了
//            String sql2 = "select safetyplanid from gq_safetyplan where safetyplannum =" + safetyplannum;
//            List<Map<String, Object>> safetyplanList = handlerService.findBySql(sql2);
//            if (CollectionUtils.isNotEmpty(safetyplanList)) {
//                String sql = "";
//                handlerService.updateBySql(sql);
//                String sql3 = "";
//                handlerService.updateBySql(sql3);
//
//            }
//        }

    }

    //
    public void beforDelete(String app, String objName, Long id, boolean isLogic) {
        if ("gq_splexiconlink".equals(objName)) {
            //把危险编号查出来，
            String sql = "select hazardnum from gq_splexiconlink where id = "+id;

            List<Map<String, Object>> mapList = handlerService.findBySql(sql);
            if(mapList.size()>0){

                //删除危险子表--预防措施信息
                String hazardnum= mapList.get(0).get("hazardnum").toString();
                String sql2 = "delete from gq_safetymeasure where hazardnum='"+hazardnum+"'";
                handlerService.updateBySql(sql2);
                //删除危险子表--隔离信息
                String sql3 = "delete from gq_tagoutline where hazardnum='"+hazardnum+"'";
                handlerService.updateBySql(sql3);
            }


        }

    }
}
